package com.sun.enhancespringdatajpa.repostory;

import com.sun.enhancespringdatajpa.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import javax.transaction.Transactional;
import java.util.List;


/**
 * 有条件分页查询可用QueryByExampleExecutor中的方法
 * 无条件分页查询可以使用PagingAndSortingRepository中的方法
 * PagingAndSortingRepository<User,Integer>, QueryByExampleExecutor<User>
 */
public interface UserRepository extends JpaRepository<User, Integer>, JpaSpecificationExecutor<User> {

    //自定义的一些查询操作
    @Query("select u from User u where u.nickName like concat('%',:name,'%') ")
    List<User> findUserInfoByLikeName(@Param("name") String name);

    @Transactional
    @Modifying
    @Query("update User  u set u.nickName=:#{#user.nickName} where u.id=:#{#user.id}")
    void updateUserInfoById(User user);

}
